{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_13676\\353215501.py:4: DeprecationWarning: The 'timeout' parameter is deprecated in favor of 'request_timeout'\n",
      "  es = Elasticsearch(hosts=\"http://192.168.2.46:9200\",timeout=3600)\n"
     ]
    }
   ],
   "source": [
    "from elasticsearch import Elasticsearch\n",
    "\n",
    "# 实例化一个ip为localhost，端口为9200，允许超时一小时的es对象\n",
    "es = Elasticsearch(hosts=\"http://192.168.2.46:9200\",timeout=3600)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\conda\\envs\\chatglm2\\lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n",
      "d:\\conda\\envs\\chatglm2\\lib\\site-packages\\transformers\\utils\\hub.py:124: FutureWarning: Using `TRANSFORMERS_CACHE` is deprecated and will be removed in v5 of Transformers. Use `HF_HOME` instead.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "from langchain.embeddings.huggingface import HuggingFaceEmbeddings\n",
    "embedding = HuggingFaceEmbeddings(model_name=r\"E:\\models\\BAAIbge-large-zh-v1.5\",\n",
    "                                            model_kwargs={'device': \"cuda\"})\n",
    "# loaders "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ObjectApiResponse({'_index': 'my_index', '_id': '10', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 10, '_primary_term': 1})"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# es.delete(index='test',id='1')\n",
    "index_name = \"my_index\"\n",
    "document = {\n",
    "    \"title\": \"Elasticsearch Tutorial\",\n",
    "    \"content\": \"Learn how to use with Python.\",\n",
    "    \"aaa\":\"2\"\n",
    "}\n",
    "\n",
    "es.index(index=index_name, id=10, body=document)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_13676\\2594806310.py:24: DeprecationWarning: Passing transport options in the API method is deprecated. Use 'Elasticsearch.options()' instead.\n",
      "  bulk_response = es.bulk(index=index_name, body=actions, request_timeout=60)\n"
     ]
    },
    {
     "ename": "BadRequestError",
     "evalue": "BadRequestError(400, 'illegal_argument_exception', 'Malformed action/metadata line [1], expected field [create], [delete], [index] or [update] but found [_op_type]')",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mBadRequestError\u001b[0m                           Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[33], line 24\u001b[0m\n\u001b[0;32m      7\u001b[0m actions \u001b[39m=\u001b[39m [\n\u001b[0;32m      8\u001b[0m     {\n\u001b[0;32m      9\u001b[0m         \u001b[39m\"\u001b[39m\u001b[39m_op_type\u001b[39m\u001b[39m\"\u001b[39m: \u001b[39m\"\u001b[39m\u001b[39mindex\u001b[39m\u001b[39m\"\u001b[39m,  \u001b[39m# 或者 \"index\" 如果允许替换\u001b[39;00m\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m     20\u001b[0m     \u001b[39m# 添加更多文档...\u001b[39;00m\n\u001b[0;32m     21\u001b[0m ]\n\u001b[0;32m     23\u001b[0m \u001b[39m# 执行批量操作\u001b[39;00m\n\u001b[1;32m---> 24\u001b[0m bulk_response \u001b[39m=\u001b[39m es\u001b[39m.\u001b[39;49mbulk(index\u001b[39m=\u001b[39;49mindex_name, body\u001b[39m=\u001b[39;49mactions, request_timeout\u001b[39m=\u001b[39;49m\u001b[39m60\u001b[39;49m)\n\u001b[0;32m     26\u001b[0m \u001b[39m# 检查结果\u001b[39;00m\n\u001b[0;32m     27\u001b[0m \u001b[39mif\u001b[39;00m bulk_response[\u001b[39m'\u001b[39m\u001b[39merrors\u001b[39m\u001b[39m'\u001b[39m]:\n",
      "File \u001b[1;32md:\\conda\\envs\\chatglm2\\lib\\site-packages\\elasticsearch\\_sync\\client\\utils.py:446\u001b[0m, in \u001b[0;36m_rewrite_parameters.<locals>.wrapper.<locals>.wrapped\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m    443\u001b[0m         \u001b[39mexcept\u001b[39;00m \u001b[39mKeyError\u001b[39;00m:\n\u001b[0;32m    444\u001b[0m             \u001b[39mpass\u001b[39;00m\n\u001b[1;32m--> 446\u001b[0m \u001b[39mreturn\u001b[39;00m api(\u001b[39m*\u001b[39margs, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs)\n",
      "File \u001b[1;32md:\\conda\\envs\\chatglm2\\lib\\site-packages\\elasticsearch\\_sync\\client\\__init__.py:711\u001b[0m, in \u001b[0;36mElasticsearch.bulk\u001b[1;34m(self, operations, body, index, error_trace, filter_path, human, pipeline, pretty, refresh, require_alias, routing, source, source_excludes, source_includes, timeout, wait_for_active_shards)\u001b[0m\n\u001b[0;32m    706\u001b[0m __body \u001b[39m=\u001b[39m operations \u001b[39mif\u001b[39;00m operations \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mNone\u001b[39;00m \u001b[39melse\u001b[39;00m body\n\u001b[0;32m    707\u001b[0m __headers \u001b[39m=\u001b[39m {\n\u001b[0;32m    708\u001b[0m     \u001b[39m\"\u001b[39m\u001b[39maccept\u001b[39m\u001b[39m\"\u001b[39m: \u001b[39m\"\u001b[39m\u001b[39mapplication/json\u001b[39m\u001b[39m\"\u001b[39m,\n\u001b[0;32m    709\u001b[0m     \u001b[39m\"\u001b[39m\u001b[39mcontent-type\u001b[39m\u001b[39m\"\u001b[39m: \u001b[39m\"\u001b[39m\u001b[39mapplication/x-ndjson\u001b[39m\u001b[39m\"\u001b[39m,\n\u001b[0;32m    710\u001b[0m }\n\u001b[1;32m--> 711\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mperform_request(  \u001b[39m# type: ignore[return-value]\u001b[39;49;00m\n\u001b[0;32m    712\u001b[0m     \u001b[39m\"\u001b[39;49m\u001b[39mPUT\u001b[39;49m\u001b[39m\"\u001b[39;49m, __path, params\u001b[39m=\u001b[39;49m__query, headers\u001b[39m=\u001b[39;49m__headers, body\u001b[39m=\u001b[39;49m__body\n\u001b[0;32m    713\u001b[0m )\n",
      "File \u001b[1;32md:\\conda\\envs\\chatglm2\\lib\\site-packages\\elasticsearch\\_sync\\client\\_base.py:320\u001b[0m, in \u001b[0;36mBaseClient.perform_request\u001b[1;34m(self, method, path, params, headers, body)\u001b[0m\n\u001b[0;32m    317\u001b[0m         \u001b[39mexcept\u001b[39;00m (\u001b[39mValueError\u001b[39;00m, \u001b[39mKeyError\u001b[39;00m, \u001b[39mTypeError\u001b[39;00m):\n\u001b[0;32m    318\u001b[0m             \u001b[39mpass\u001b[39;00m\n\u001b[1;32m--> 320\u001b[0m     \u001b[39mraise\u001b[39;00m HTTP_EXCEPTIONS\u001b[39m.\u001b[39mget(meta\u001b[39m.\u001b[39mstatus, ApiError)(\n\u001b[0;32m    321\u001b[0m         message\u001b[39m=\u001b[39mmessage, meta\u001b[39m=\u001b[39mmeta, body\u001b[39m=\u001b[39mresp_body\n\u001b[0;32m    322\u001b[0m     )\n\u001b[0;32m    324\u001b[0m \u001b[39m# 'X-Elastic-Product: Elasticsearch' should be on every 2XX response.\u001b[39;00m\n\u001b[0;32m    325\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_verified_elasticsearch:\n\u001b[0;32m    326\u001b[0m     \u001b[39m# If the header is set we mark the server as verified.\u001b[39;00m\n",
      "\u001b[1;31mBadRequestError\u001b[0m: BadRequestError(400, 'illegal_argument_exception', 'Malformed action/metadata line [1], expected field [create], [delete], [index] or [update] but found [_op_type]')"
     ]
    }
   ],
   "source": [
    "from elasticsearch import Elasticsearch\n",
    "\n",
    "# 假设已经建立了Elasticsearch客户端实例es\n",
    "index_name = 'my_index'\n",
    "\n",
    "# 创建一个批量操作的列表，每个操作是一个字典，包含\"_index\"，\"_type\"（在7.x之后默认为\"_doc\"），\"_id\"（可选）和\"doc\"（文档内容）\n",
    "actions = [\n",
    "    {\n",
    "        \"_op_type\": \"index\",  # 或者 \"index\" 如果允许替换\n",
    "        \"_index\": index_name,\n",
    "        \"_id\": \"doc1\",\n",
    "        '_source': {'question': 'The life, universe and everything.'}\n",
    "    },\n",
    "    {\n",
    "        \"_op_type\": \"index\",  # 另一个文档\n",
    "        \"_index\": index_name,\n",
    "        \"_id\": \"doc2\",\n",
    "       '_source': {'question': 'The life, universe and everything.'}\n",
    "    },\n",
    "    # 添加更多文档...\n",
    "]\n",
    "\n",
    "# 执行批量操作\n",
    "bulk_response = es.bulk(index=index_name, body=actions, request_timeout=60)\n",
    "\n",
    "# 检查结果\n",
    "if bulk_response['errors']:\n",
    "    print(\"Some items failed:\", bulk_response['items'])\n",
    "else:\n",
    "    print(\"All items were successfully indexed\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'title': 'Elasticsearch Tutorial', 'content': 'Learn how to use with Python.', 'aaa': '2'}\n",
      "{'title': 'Elasticsearch Tutorial', 'content': 'Learn how to use Elasticsearch with Python.'}\n"
     ]
    }
   ],
   "source": [
    "query = {\n",
    "    \"query\": {\n",
    "        \"match\": {\n",
    "            \"content\": \"Python\"\n",
    "        }\n",
    "    }\n",
    "}\n",
    "response = es.search(index=index_name, body=query)\n",
    "\n",
    "for hit in response['hits']['hits']:\n",
    "    print(hit[\"_source\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "chatglm2",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
